package cn.edu.scau.dao;

import cn.edu.scau.entity.RecoveryTrack;
import java.util.Date;

import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@CacheNamespace
@Repository
public interface RecoveryTrackMapper {
	/**
	 *  根据编号和日期删除康复登记
	 * @param recoverytrackdate
	 * @param recoveryregisterid
	 * @return
	 */
    @Delete({
        "delete from recoverytrack",
        "where recoveryTrackDate = #{recoverytrackdate,jdbcType=DATE}",
          "and recoveryRegisterID = #{recoveryregisterid,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(@Param("recoverytrackdate") Date recoverytrackdate, @Param("recoveryregisterid") Integer recoveryregisterid);

    /**
     * 根据日期、康复跟踪编号、康复跟踪记录、康复跟踪客户状态新增康复跟踪信息，不考虑null
     * @param record
     * @return
     */
    @Insert({
        "insert into recoverytrack (recoveryTrackDate, recoveryRegisterID, ",
        "recoveryTrackRecording, recoveryTrackCustomerStatus)",
        "values (#{recoverytrackdate,jdbcType=DATE}, #{recoveryregisterid,jdbcType=INTEGER}, ",
        "#{recoverytrackrecording,jdbcType=VARCHAR}, #{recoverytrackcustomerstatus,jdbcType=VARCHAR})"
    })
    int insert(RecoveryTrack record);

    /**
     * 根据日期、康复跟踪编号、康复跟踪记录、康复跟踪客户状态新增康复跟踪信息，考虑null。若为空，不向数据库插入数据
     * @param record
     * @return
     */
    @InsertProvider(type=RecoveryTrackSqlProvider.class, method="insertSelective")
    int insertSelective(RecoveryTrack record);

    /**
     * 根据康复跟踪编号和日期查找信息
     * @param recoverytrackdate
     * @param recoveryregisterid
     * @return
     */
    @Select({
        "select",
        "recoveryTrackDate, recoveryRegisterID, recoveryTrackRecording, recoveryTrackCustomerStatus",
        "from recoverytrack",
        "where recoveryTrackDate = #{recoverytrackdate,jdbcType=DATE}",
          "and recoveryRegisterID = #{recoveryregisterid,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="recoveryTrackDate", property="recoveryTrackDate", jdbcType=JdbcType.DATE, id=true),
        @Result(column="recoveryRegisterID", property="recoveryRegisterID", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="recoveryTrackRecording", property="recoveryTrackRecording", jdbcType=JdbcType.VARCHAR),
        @Result(column="recoveryTrackCustomerStatus", property="recoveryTrackCustomerStatus", jdbcType=JdbcType.VARCHAR)
    })
    RecoveryTrack selectByPrimaryKeyNoCustomerName(@Param("recoverytrackdate") Date recoverytrackdate, @Param("recoveryregisterid") Integer recoveryregisterid);

    /**
     * 根据护士编号查找跟踪信息
     * @param nurseID
     * @return
     */
    @Select({
        "select",
        "recoveryTrackDate, recoveryRegisterID, customerName, recoveryTrackRecording, recoveryTrackCustomerStatus",
        "from recoverytrack natural join recoveryregister natural join customer natural join nurse",
        "where nurseID = #{nurseID,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="recoveryTrackDate", property="recoveryTrackDate", jdbcType=JdbcType.DATE, id=true),
        @Result(column="recoveryRegisterID", property="recoveryRegisterID", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="customerName", property="customerName", jdbcType=JdbcType.VARCHAR),
        @Result(column="recoveryTrackRecording", property="recoveryTrackRecording", jdbcType=JdbcType.VARCHAR),
        @Result(column="recoveryTrackCustomerStatus", property="recoveryTrackCustomerStatus", jdbcType=JdbcType.VARCHAR)
    })
    List<RecoveryTrack> selectWithNurseID(Integer nurseID);
    
    
    @Select({
        "select",
        "recoveryTrackDate, recoveryRegisterID, customerName, recoveryTrackRecording, recoveryTrackCustomerStatus",
        "from recoverytrack natural join recoveryregister natural join customer",
        "where recoveryTrackDate = #{recoverytrackdate,jdbcType=DATE}",
          "and recoveryRegisterID = #{recoveryregisterid,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="recoveryTrackDate", property="recoveryTrackDate", jdbcType=JdbcType.DATE, id=true),
        @Result(column="recoveryRegisterID", property="recoveryRegisterID", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="customerName", property="customerName", jdbcType=JdbcType.VARCHAR),
        @Result(column="recoveryTrackRecording", property="recoveryTrackRecording", jdbcType=JdbcType.VARCHAR),
        @Result(column="recoveryTrackCustomerStatus", property="recoveryTrackCustomerStatus", jdbcType=JdbcType.VARCHAR)
    })
    RecoveryTrack selectByPrimaryKey(@Param("recoverytrackdate") Date recoverytrackdate, @Param("recoveryregisterid") Integer recoveryregisterid);
    
    /**
     * 根据康复跟踪编号查询信息
     * @param recoveryregisterid
     * @return
     */
    @Select({
        "select",
        "recoveryTrackDate, recoveryRegisterID, recoveryTrackRecording, recoveryTrackCustomerStatus",
        "from recoverytrack",
        "where recoveryRegisterID = #{recoveryregisterid,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="recoveryTrackDate", property="recoveryTrackDate", jdbcType=JdbcType.DATE, id=true),
        @Result(column="recoveryRegisterID", property="recoveryRegisterID", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="recoveryTrackRecording", property="recoveryTrackRecording", jdbcType=JdbcType.VARCHAR),
        @Result(column="recoveryTrackCustomerStatus", property="recoveryTrackCustomerStatus", jdbcType=JdbcType.VARCHAR)
    })
    List<RecoveryTrack> selectByRecoveryRegisterID(Integer recoveryregisterid);
    
    /**
     * 查找全部，不包括康复登记ID
     * @return
     */
    @Select({
        "select",
        "recoveryTrackDate, recoveryRegisterID, customerName,recoveryTrackRecording, recoveryTrackCustomerStatus",
        "from recoverytrack"
    })
    List<RecoveryTrack> selectAllNoCustomerName();
    
    
    @Select({
        "select",
        "recoveryTrackDate, recoveryRegisterID, customerName, recoveryTrackRecording, recoveryTrackCustomerStatus",
        "from recoverytrack natural join recoveryregister natural join customer"
    })
    List<RecoveryTrack> selectAll();
    
    
    /**
     * 根据日期、康复跟踪编号、康复跟踪记录、康复跟踪客户状态修改登记信息,考虑null。若为空，不会更改数据库数据
     * @param record
     * @return
     */
    @UpdateProvider(type=RecoveryTrackSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(RecoveryTrack record);

    /**
     * 根据日期、康复跟踪编号、康复跟踪记录、康复跟踪客户状态修改登记信息,不考虑null。若为空，会将对应属性修改为空
     * @param record
     * @return
     */
    @Update({
        "update recoverytrack",
        "set recoveryTrackRecording = #{recoverytrackrecording,jdbcType=VARCHAR},",
          "recoveryTrackCustomerStatus = #{recoverytrackcustomerstatus,jdbcType=VARCHAR}",
        "where recoveryTrackDate = #{recoverytrackdate,jdbcType=DATE}",
          "and recoveryRegisterID = #{recoveryregisterid,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(RecoveryTrack record);
    
    /**
     * 根据记录日期、康复登记编号、客户名组合查询
     * @param recoveryTrackDate
     * @param recoveryRegisterID
     * @param customerName
     * @return
     */
    @SelectProvider(type = RecoveryTrackSqlProvider.class,method = "find")
    List<RecoveryTrack> find(@Param("recoveryTrackDate") Date recoveryTrackDate,@Param("recoveryRegisterID") Integer recoveryRegisterID,@Param("customerName") String customerName);
    
    /**
     * 根据记录日期、康复登记编号、客户名组合查询，康复登记中护士编号已确定
     * @param recoveryTrackDate
     * @param recoveryRegisterID
     * @param customerName
     * @param nurseID
     * @return
     */
    @SelectProvider(type = RecoveryTrackSqlProvider.class,method = "findWithNurseID")
    List<RecoveryTrack> findWithNurseID(@Param("recoveryTrackDate") Date recoveryTrackDate,@Param("recoveryRegisterID") Integer recoveryRegisterID,@Param("customerName") String customerName,@Param("nurseID") Integer nurseID);
}